package com.atguigu.tingshu.order.strategy.impl;
import java.math.BigDecimal;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.user.client.VipServiceConfigFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.google.common.collect.Lists;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * vip购买进入到结算页所对应的策略实现类
 */
@Service
@Slf4j
public class VipOrderTradeStrategy implements OrderTradeStrategy {

    @Autowired
    private VipServiceConfigFeignClient vipServiceConfigFeignClient ;

    @Autowired
    private RedisTemplate<String , String> redisTemplate;

    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {

        // 创建一个OrderInfoVo对象，封装结算页所需要的数据
        OrderInfoVo orderInfoVo = new OrderInfoVo() ;

        // 根据付款项目的id查询付款项目
        Long itemId = tradeVo.getItemId();
        Result<VipServiceConfig> serviceConfigResult = vipServiceConfigFeignClient.getById(itemId);
        VipServiceConfig vipServiceConfig = serviceConfigResult.getData();

        BigDecimal originalAmount = vipServiceConfig.getPrice();
        BigDecimal discountPrice = vipServiceConfig.getDiscountPrice();
        orderInfoVo.setOriginalAmount(originalAmount);     // 原价
        orderInfoVo.setDerateAmount(originalAmount.subtract(discountPrice));
        orderInfoVo.setOrderAmount(discountPrice);    // 折扣价

        // 设置订单明细列表数据
        OrderDetailVo orderDetailVo = new OrderDetailVo() ;
        orderDetailVo.setItemId(itemId);
        orderDetailVo.setItemName(vipServiceConfig.getName());
        orderDetailVo.setItemUrl(vipServiceConfig.getImageUrl());
        orderDetailVo.setItemPrice(discountPrice);
        orderInfoVo.setOrderDetailVoList(Arrays.asList(orderDetailVo));

        // 设置减免明细列表
        BigDecimal derateAmount = orderInfoVo.getDerateAmount();
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>() ;
        if(derateAmount.doubleValue() > 0) {
            OrderDerateVo orderDerateVo = new OrderDerateVo() ;
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_VIP_SERVICE_DISCOUNT);
            orderDerateVo.setDerateAmount(derateAmount);
            orderDerateVo.setRemarks("vip服务折扣");
            orderDerateVoList.add(orderDerateVo) ;
        }
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);

        // 设置时间戳和签名
        this.setOtherData(orderInfoVo , tradeVo , redisTemplate);

        // 返回
        return orderInfoVo;
    }

    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_VIP;  // 1003
    }

}
